package cn.edu.gues.infosystam.service.impl;

import cn.edu.gues.infosystam.entity.Report;
import cn.edu.gues.infosystam.entity.Student;
import cn.edu.gues.infosystam.entity.Task;
import cn.edu.gues.infosystam.mapper.ReportMapper;
import cn.edu.gues.infosystam.service.ReportService;
import cn.edu.gues.infosystam.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 苏小小
 * @since 2020-06-11
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Autowired
    private StudentService studentService;

    @Override
    public IPage<Task> getReportTask(Page<Task> page, String sno) {
        Student student = studentService.getStudent(sno);
        if (student == null)
            return null;

        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        queryWrapper.eq("task.task_state","1");
        queryWrapper.eq("task.deleted","0");
        // 显示到秒的时间
        String date = DateFormat.getDateTimeInstance().format(new Date());
        String college = student.getCollege();
        String classes = student.getCname();
        queryWrapper.last("and (task.task_id NOT IN (SELECT report.task_id FROM report where report.sno = '"+
                student.getSno() +"' and report.task_id = task.task_id )) and ('"+
                date +"' between task.task_start and task.task_end) and (task.task_range REGEXP '{\"college\":\""+
                college +"\",\"value\":\"all\"}|\""+ classes +"\"' or task.task_range = '\"all\"')  ORDER BY task_id DESC");

        return this.baseMapper.getReportTask(new Page<Task>(1, 10), queryWrapper);
    }

    @Override
    public List<Report> getReportAll(Integer taskId) {
        return this.baseMapper.getReportAll(taskId);
    }


}
